package bom.example.common.utils;



import java.io.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 序列化工具
 */
public class SerializaUtils {

    /**
     * 序列化对象
     *
     * @param obj 对象
     * @return 序列化后的字节数组
     * @throws IOException
     */
    public static byte[] serialize(Object obj) throws IOException {
        if (null == obj) {
            return null;
        }

        try (
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ObjectOutputStream out = new ObjectOutputStream(byteArrayOutputStream);
        ) {

            out.writeObject(obj);
            return byteArrayOutputStream.toByteArray();
        }

    }

    /**
     * 反序列化
     *
     * @param bytes 对象序列化后的字节数组
     * @return 反序列化后的对象
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
        if (null == bytes) {
            return null;
        }

        try (
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
                ObjectInputStream in = new ObjectInputStream(byteArrayInputStream);
        ) {

            return in.readObject();
        }
    }

    /**
     * 反序列化成指定类型的对象
     *
     * @param bytes 对象序列化后的字节数组
     * @param c 反序列化后的对象类型
     * @return 指定类型的对象
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static <T> T deserialize(byte[] bytes, Class<T> c) throws ClassNotFoundException, IOException {
        return c.cast(deserialize(bytes));
    }




    public static <T> void wirteList(String key, List<T> ts,String fileName){
        wirteMapObject(key,ts,fileName);
    }



    public static <T> Map<String, Object> beanToMap(T t){
        Class<?> cls= t.getClass();
        Field[] fields=cls.getDeclaredFields();
        System.out.println(fields.length);
        Map<String, Object> map=new HashMap();
        try {
            for (int i = 0; i < fields.length; i++) {
                Field field=fields[i];
                field.setAccessible(true);
                Object value=field.get(t);
                System.out.println(value);
                map.put(field.getName(),value);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }


    public static void getMapObject(){
        File file=PathUtils.getPathByName("test","test1.in");
        System.out.println(file.getPath());
        Object obj=deSerializeObj(file);
        System.out.println(obj);
    }

    public static void wirteMapObject(String key,Object value,String path){
        Map<String, Object> stringObjectMap=new HashMap<>();
        stringObjectMap.put(key,value);
        File file= PathUtils.getPathByName("test",path);
        System.out.println(file.getPath());
        SerializeObj(stringObjectMap,file);
    }


    private static void SerializeObj(Object obj,File file) {
        ObjectOutputStream oo=null;
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream(file);
             oo=new ObjectOutputStream(fos);
             oo.writeObject(obj);
            oo.flush();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(oo!=null){
                try {
                    oo.close();
                    System.out.println("关闭");
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try {
                    fos.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }


    private static Object deSerializeObj(File file){
        ObjectInputStream ois=null;
        FileInputStream fis=null;
        try {
            fis=new FileInputStream(file);
            ois=new ObjectInputStream(fis);
            Object o = ois.readObject();
            return o;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(ois!=null){
                try {
                    ois.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(fis!=null){
                try {
                    fis.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}
